import { Role } from "@/api";

// 加载角色列表
let role_list = async (context, payload) => {
    let { status, msg, data } = await Role.list({ pagesize: 10, pageindex: 1 });
    if (status) {
        context.commit('ROLE_LIST', data);
    }
    return { status, msg, data };
}

// 根据角色id，获取角色的路由权限
let load_route_permission = async (context, payload) => {
    let { status, msg, data } = await Role.route_power(payload);
    if (status) {
        context.commit('LOAD_ROUTE_PERMISSION', data);
    }
    return { status, msg, data };
}

// 添加角色
let add_role = async (context, playload) => {
    let { status, msg, data } = await Role.add(playload);
    if (status) {
        context.commit('ADD_ROLE', { ...data, ...playload });
    }
    // 尽量在组件中实现跳转。弹窗等功能；return出去的是Promise对象
    return { status, msg }
}

// 编辑角色
let edit_role = async (context, payload) => {
    let { role_id } = payload;
    let { status, msg } = await Role.update(role_id, payload);
    if (status) {
        context.commit('EDIT_ROLE', payload);
    }
    return { status, msg }
}

// 删除角色
let remove_role = async (context, payload) => {
    let { role_id, index } = payload;
    let { status, msg } = await Role.remove(role_id);
    if (status) {
        // 索引从哪里来？点击删除按钮传参
        context.commit('REMOVE_ROLE', index);
    }
    // 尽量在组件中实现跳转。弹窗等功能；return出去的是Promise对象
    return { status, msg }
}

// 根据角色id，获取角色的操作按钮权限
let load_operation_permission = async (context, payload) => {
    let { status, msg, data } = await Role.menu_power(payload);
    if (status) {
        context.commit('LOAD_OPERATION_PERMISSION', data);
    }
    return { status, msg, data };
}

export default {
    role_list,
    load_route_permission,
    add_role,
    edit_role,
    remove_role,
    load_operation_permission,
}